<?php

class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{

    protected $_name = 'Users';
    protected $_primary = 'id';
    
	public function getUser($id)
    {
    	$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Could not find row $id");
		}
		return $row->toArray();
    }
    
    public function getUserID($username)
    {
    	$username = (string)$username;
    	$row = $this->fetchRow("Username = '" . $username . "'");
    	if (!$row) {
			throw new Exception("Could not find row $username");
		}
		return $row['id'];
    }
    
	public function getUserLogin($email,$password)
    {    	
		$row = $this->fetchRow("Email ='" . $email."' and Password='".$password."'");		
		if (!$row) {
			throw new Exception("Could not user with email  $email");
		}
		return $row->toArray();
    }
    
	public function getUserByEmailOrUsername($email,$username)
    {    	
		$row = $this->fetchRow("Email ='" . $email."' OR Username='".$username."'");		
		if (!$row) {
			return null;
		}
		return $row->toArray();
    }
    
    
	public function addUser($email, $password, $usertype, $username, $active, $photo)
	{
		$data = array(
			'Email' => $email,
			'Password' => $password,
			'UserType' => $usertype,
			'Username' => $username,
			'Active' => $active,
			'Photo_link' => $photo,
			'Token'=>0,			
		);
		$this->insert($data);
	}
	
	public function editUserToken($id)
	{
		$data = array(
			'Token' => 1,		
		);
		$this->update($data,  'id = '. (int)$id);
	}
	
	
	public function editUser($id, $email, $password, $usertype, $photo)
	{
		//change password, e-mail, usertype or photo.
		$data = array(
			'Email' => $email,
			'Password' => $password,
			'UserType' => $usertype,
			'Photo_link' => $photo,			
		);
		$this->update($data,  'id = '. (int)$id);
	}
	
	public function activateAcc($id)
	{
		$data = array('Active' => '1');
		$this->update($data,  'id = '. (int)$id);
	}
	
	public function deactivateAcc($id)
	{
		$data = array('Active' => '0');
		$this->update($data,  'id = '. (int)$id);
	}
	
	public function addBasicInfo($id, $basicInfoID)
	{
		$data = array('BasicInfo_id' => $basicInfoID);
		$this->update($data,  'id = '. (int)$id);
	}
	
	public function addInterestInfo($id, $interestInfoID)
	{
		$data = array('InterestInfo_id' => $interestInfoID);
		$this->update($data,  'id = '. (int)$id);
	}
	
	public function addMedicalInfo($id, $medicalInfoID)
	{
		$data = array('MedicalInfo_id' => $medicalInfoID);
		$this->update($data,  'id = '. (int)$id);
	}
}

